package com.example.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
//import com.example.demo.common.R;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.demo.common.BaseContext;
import com.example.demo.common.S;
import com.example.demo.entity.Employee;
import com.example.demo.mapper.EmployeeMapper;
import com.example.demo.service.EmployeeService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.apache.tomcat.util.security.MD5Encoder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.util.DigestUtils;
//import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.lang.invoke.LambdaMetafactory;
import java.time.LocalDateTime;

@Slf4j
@RestController
@RequestMapping("/employee")
public class  EmployeeController {
    @Autowired
    EmployeeService employeeService;
    //员工登录
    @PostMapping("/login")
    public S<Employee> login(HttpServletRequest request, @RequestBody Employee employee){
        //将页面提交的密码进行md5加密
        String password = employee.getPassword();
        password = DigestUtils.md5DigestAsHex(password.getBytes());
        //根据用户名查询数据库
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper<>();
        lambdaQueryWrapper.like(Employee::getUsername,employee.getUsername());
        Employee one = employeeService.getOne(lambdaQueryWrapper);
        //如果没有查询到就返回登陆失败结果
        if(one==null){
            return S.error("登录失败");
        }
       if (!one.getPassword().equals(password)){
           return S.error("登录失败");
       }
       if (one.getStatus()==0){
           return S.error("员工已禁用");
       }
        HttpSession session = request.getSession();
       session.setAttribute("employee",one.getId());
       return S.success(one);
    }
    //员工退出
    @PostMapping("/logout")
    public S<String> logout(HttpServletRequest request){
        HttpSession session = request.getSession();
        session.removeAttribute("employee");
        return S.success("退出成功");
    }
    @PostMapping
    public S<String> save(HttpServletRequest request, @RequestBody Employee employee){
        employee.setPassword(DigestUtils.md5DigestAsHex("123456".getBytes()));
//        employee.setCreateTime(LocalDateTime.now());
//        employee.setUpdateTime(LocalDateTime.now());
        Long employee1 = (Long) request.getSession().getAttribute("employee");
//        employee.setCreateUser(employee1);
//        employee.setUpdateUser(employee1);
        employeeService.save(employee);
        return S.success("新增员工成功");
    }
    //员工信息的分页查询
    //默认用params接收数据
    @GetMapping("/page")
    public S<Page<Employee>> page( int page, int pageSize, String name){
        log.info(page+pageSize+name);
        //构造分页构造器
        Page<Employee> iPage=new Page(page,pageSize);
        //构造条件构造器
        LambdaQueryWrapper<Employee> lambdaQueryWrapper=new LambdaQueryWrapper();
        //添加过滤条件
        lambdaQueryWrapper.like(StringUtils.isNotEmpty(name),Employee::getName,name);
        //添加排序条件
        lambdaQueryWrapper.orderByDesc(Employee::getUpdateTime);
        //执行插讯，查到的结果分装进入iPage中
        employeeService.page(iPage, lambdaQueryWrapper);
        //执行查询
        return S.success(iPage);
    }
    //根据id修改
    //获取到的id前端对于long只有16位后面的四舍五入
    @PutMapping
    public S<String> update(HttpServletRequest request,@RequestBody Employee employee){
//        employee.setUpdateUser((Long) request.getSession().getAttribute("employee"));
//        employee.setUpdateTime(LocalDateTime.now());
        log.info("2线程序号"+ Thread.currentThread().getId() );
        log.info("获取的id"+String.valueOf(BaseContext.getCurrentId()));
        employeeService.updateById(employee);
        return S.success("修改成功");
    }
    //根据id查询员工信息
    @GetMapping("/{id}")
    public S<Employee> getById(@PathVariable Long id){
        log.info(String.valueOf(id));
        Employee byId = employeeService.getById(id);
        return S.success(byId);
    }
}
